package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.GameManagerFragmentViewPagerAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.eventbus.MessageEvent;
import com.windoent.yugioh.ui.fragment.mygame.MyGameAllFragment;
import com.windoent.yugioh.ui.fragment.mygame.MyGameHasEndedFragment;
import com.windoent.yugioh.ui.fragment.mygame.MyGameOnGoingFragment;
import com.windoent.yugioh.ui.fragment.mygame.MyGameStayInFragment;
import com.windoent.yugioh.ui.fragment.mygame.MyGameToBePaidFragment;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.popupwindow.MeInfoAuthPopUpWindow;
import com.youth.banner.util.BannerUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * author : robert
 * date   : 2021/5/10 16:42
 * desc   :比赛记录
 */
public class MyGameRecordActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private LinearLayout mLlFiltrate;
    private FrameLayout mViewpagerLinear;
    private MagicIndicator mMagicIndicator;
    private ViewPager mViewpager;
    //标题头
    private List<String> fragmentsTitle = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private GameManagerFragmentViewPagerAdapter pagerAdapter;
    private CommonNavigatorAdapter adapter;
    private CommonNavigator commonNavigator;

    @Override
    public int getLayout() {
        return R.layout.activity_game_record;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mLlFiltrate = (LinearLayout) findViewById(R.id.ll_filtrate);
        mViewpagerLinear = (FrameLayout) findViewById(R.id.viewpager_linear);
        mMagicIndicator = (MagicIndicator) findViewById(R.id.magicIndicator);
        mViewpager = (ViewPager) findViewById(R.id.viewpager);
    }

    @Override
    public void initData() {
        super.initData();
        //magic设置
        magic();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mLlFiltrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //筛选
                showFiltrate();
            }
        });
    }

    private void showFiltrate() {
        new MeInfoAuthPopUpWindow(MyGameRecordActivity.this, getFiltData(), BannerUtils.dp2px(16), new MeInfoAuthPopUpWindow.OnInterface() {
            @Override
            public void onSelect(String s) {
                //发送eventbus粘性事件
                MessageEvent event = new MessageEvent();
                event.setMessage(getFiltType(s));
                event.setType(mViewpager.getCurrentItem());
                EventBus.getDefault().postSticky(event);
            }
        }).showAtBottom(mLlFiltrate, 0, 0);
    }

    //筛选
    private String getFiltType(String name) {
        if (BaseMethod.notNull(name) && name.equals(getString(R.string.string_all))) {
            return "0";
        }
        if (BaseMethod.notNull(name) && name.equals(getString(R.string.string_entertainment_race))) {
            return "1";
        }
        if (BaseMethod.notNull(name) && name.equals(getString(R.string.string_point_race))) {
            return "2";
        }
        if (BaseMethod.notNull(name) && name.equals(getString(R.string.string_teshu_race))) {
            return "3";
        }
        if (BaseMethod.notNull(name) && name.equals(getString(R.string.string_xunhui_race))) {
            return "4";
        }
        return "0";
    }

    //0 全部 1娱乐 2积分 3特殊赛 4 巡回赛
    private List<String> getFiltData() {
        List<String> list = new ArrayList<>();
        list.clear();
        list.add(getString(R.string.string_all));
        list.add(getString(R.string.string_entertainment_race));
        list.add(getString(R.string.string_point_race));
        list.add(getString(R.string.string_teshu_race));
        list.add(getString(R.string.string_xunhui_race));
        return list;
    }

    private void magic() {
        getInitData();
        setMagicIndicator();
        if (pagerAdapter == null) {
            pagerAdapter = new GameManagerFragmentViewPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, MyGameRecordActivity.this, fragmentsTitle, fragments);
            if (mViewpager != null)
                //The application's PagerAdapter changed the adapter's contents without calling PagerAdapter
                if (mViewpager.getAdapter() != null) {
                    pagerAdapter.notifyDataSetChanged();
                } else
                    mViewpager.setAdapter(pagerAdapter);
        } else {
            pagerAdapter.notifyDataSetChanged();
        }
    }

    private void getInitData() {
        if (fragmentsTitle != null) {
            fragmentsTitle.clear();
            fragmentsTitle.add(getString(R.string.string_all));
            fragmentsTitle.add(getString(R.string.string_tobe_paid));
            fragmentsTitle.add(getString(R.string.string_to_stay_in));
            fragmentsTitle.add(getString(R.string.string_ongoing));
            fragmentsTitle.add(getString(R.string.string_has_ended));
        }
        if (fragments != null) {
            fragments.clear();
            fragments.add(MyGameAllFragment.newsInstance());
            fragments.add(MyGameToBePaidFragment.newsInstance());
            fragments.add(MyGameStayInFragment.newsInstance());
            fragments.add(MyGameOnGoingFragment.newsInstance());
            fragments.add(MyGameHasEndedFragment.newsInstance());
        }
    }

    private void setMagicIndicator() {
        if (commonNavigator == null) {
            commonNavigator = new CommonNavigator(MyGameRecordActivity.this);
            commonNavigator.setAdjustMode(true);//均分
            CommonNavigatorAdapter adapterNavigstor = new CommonNavigatorAdapter() {
                @Override
                public int getCount() {
                    return fragmentsTitle == null ? 0 : fragmentsTitle.size();
                }

                @Override
                public IPagerTitleView getTitleView(Context context, int index) {
                    ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(MyGameRecordActivity.this);
                    titleView.setText(Html.fromHtml(fragmentsTitle.get(index)));
                    titleView.setNormalColor(Color.WHITE);
                    titleView.setSelectedColor(Color.parseColor("#FFEC4E4E"));
                    titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f);
                    titleView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mViewpager.setCurrentItem(index, false);
                        }
                    });
                    return titleView;
                }

                @Override
                public IPagerIndicator getIndicator(Context context) {
                    LinePagerIndicator indicator = new LinePagerIndicator(MyGameRecordActivity.this);
                    indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                    indicator.setLineHeight(TDevice.dp2px(mContext, 5));
                    indicator.setLineWidth(TDevice.dp2px(mContext, 30));
                    indicator.setRoundRadius(TDevice.dp2px(MyGameRecordActivity.this, 6));
                    indicator.setStartInterpolator(new AccelerateInterpolator());
                    indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                    indicator.setColors(Color.parseColor("#FFEC4E4E"));
                    return indicator;
                }
            };
            commonNavigator.setAdapter(adapterNavigstor);

            if (mMagicIndicator != null)
                mMagicIndicator.setNavigator(commonNavigator);
            if (mMagicIndicator != null && mViewpager != null)
                ViewPagerHelper.bind(mMagicIndicator, mViewpager);
        } else {
            commonNavigator.notifyDataSetChanged();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "比赛记录界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "比赛记录界面");
    }
}